export const getExplore = () => {
    const Sys = {}
    const ua = navigator.userAgent.toLowerCase()
    let s;
    (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1]
        : (s = ua.match(/msie ([\d\.]+)/)) ? Sys.ie = s[1]
            : (s = ua.match(/edg\/([\d\.]+)/)) ? Sys.edge = s[1]
                : (s = ua.match(/firefox\/([\d\.]+)/)) ? Sys.firefox = s[1]
                    : (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? Sys.opera = s[1]
                        : (s = ua.match(/chrome\/([\d\.]+)/)) ? Sys.chrome = s[1]
                            : (s = ua.match(/version\/([\d\.]+).*safari/)) ? Sys.safari = s[1] : 0
    // 根据关系进行判断
    if (Sys.ie) return (`IE: ${Sys.ie}`)
    if (Sys.edge) return (`EDGE: ${Sys.edge}`)
    if (Sys.firefox) return (`Firefox: ${Sys.firefox}`)
    if (Sys.chrome) return (`Chrome: ${Sys.chrome}`)
    if (Sys.opera) return (`Opera: ${Sys.opera}`)
    if (Sys.safari) return (`Safari: ${Sys.safari}`)
    return 'Unkonwn'
}

export const browserRedirect = () => {
    const sUserAgent = navigator.userAgent
    const isWin = (navigator.platform === 'Win32') || (navigator.platform === 'Windows')
    const isMac = (navigator.platform === 'Mac68K') || (navigator.platform === 'MacPPC') || (navigator.platform === 'Macintosh') || (navigator.platform === 'MacIntel')
    if (isMac) return 'Mac'
    const isUnix = (navigator.platform === 'X11') && !isWin && !isMac
    if (isUnix) return 'Unix'
    const isLinux = (String(navigator.platform).indexOf('Linux') > -1)
    if (isLinux) return 'Linux'
    if (isWin) {
        const isWin2K = sUserAgent.indexOf('Windows NT 5.0') > -1 || sUserAgent.indexOf('Windows 2000') > -1
        if (isWin2K) return 'Win 2000'
        const isWinXP = sUserAgent.indexOf('Windows NT 5.1') > -1 || sUserAgent.indexOf('Windows XP') > -1
        if (isWinXP) return 'Win XP'
        const isWin2003 = sUserAgent.indexOf('Windows NT 5.2') > -1 || sUserAgent.indexOf('Windows 2003') > -1
        if (isWin2003) return 'Win 2003'
        const isWinVista = sUserAgent.indexOf('Windows NT 6.0') > -1 || sUserAgent.indexOf('Windows Vista') > -1
        if (isWinVista) return 'Win Vista'
        const isWin7 = sUserAgent.indexOf('Windows NT 6.1') > -1 || sUserAgent.indexOf('Windows 7') > -1
        if (isWin7) return 'Win 7'
        const isWin10 = sUserAgent.indexOf('Windows NT 10') > -1 || sUserAgent.indexOf('Windows 10') > -1
        if (isWin10) return 'Win 10'
    }
    return 'other'
}

const getIPs = (callback) => {
    const ip_dups = {}
    const RTCPeerConnection = window.RTCPeerConnection
        || window.mozRTCPeerConnection
        || window.webkitRTCPeerConnection
    const mediaConstraints = {
        optional: [{ RtpDataChannels: true }],
    }
    // 这里就是需要的ICEServer了
    const servers = {
        iceServers: [
            { urls: 'stun:stun.services.mozilla.com' },
            { urls: 'stun:stun.l.google.com:19302' },
        ],
    }
    const pc = new RTCPeerConnection(servers, mediaConstraints)
    function handleCandidate(candidate) {
        const ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
        const hasIp = ip_regex.exec(candidate)
        if (hasIp) {
            const ip_addr = ip_regex.exec(candidate)[1]
            if (ip_dups[ip_addr] === undefined) { callback(ip_addr) }
            ip_dups[ip_addr] = true
        }
    }
    // 网络协商的过程
    pc.onicecandidate = function (ice) {
        if (ice.candidate) {
            handleCandidate(ice.candidate.candidate)
        }
    }

    pc.createDataChannel('')
    // 创建一个SDP(session description protocol)会话描述协议 是一个纯文本信息 包含了媒体和网络协商的信息
    pc.createOffer((result) => {
        pc.setLocalDescription(result, () => { }, () => { })
    }, () => { })
    setTimeout(() => {
        const lines = pc.localDescription.sdp.split('\n')
        lines.forEach((line) => {
            if (line.indexOf('a=candidate:') === 0) { handleCandidate(line) }
        })
    }, 1000)
}
getIPs((ip) => {
    console.log('ip', ip)
    tempIP = ip
    debugger
})
